package com.cashbox.gui.componentes;

import com.cashbox.gui.renders.CashBoxListRender;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

/**
 *
 * @author Gustavo Enrique Ruiz Cisneros
 */
public class CashBoxList extends javax.swing.JList {

    private Font listItemFont = new Font("Calibri", Font.PLAIN, 22);
    private Color listColor = Color.BLACK;
    private Color listItemColor = new Color(0, 153, 255);
    private Color colorBackGround = new Color(50, 50, 50);
    private Color selectionColor = new Color(0, 153, 255);
    private float selectionOpacity = 0.2f,
            selectionBorderOpacity = 0.0f,
            listOpacity = 0.00f,
            listBorderOpacity = 0.0f;

    public CashBoxList() {
        super();
        init();
    }

    private void init() {
        //setOpaque(false);
        setFont(listItemFont);
        setBackground(new Color(50, 50, 50));
        //setCellRenderer(new SelectorsListCellRenderer());
        //setCellRenderer(new CashBoxListRender());
        setCellRenderer(new CashBoxListRender());
        
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        Composite composite = g2.getComposite();

        g2.setColor(colorBackGround);
        g2.fillRect(0, 0, getWidth(), getHeight());
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, listOpacity));



        //g2.setColor(listColor);

        RoundRectangle2D background;
        background = new RoundRectangle2D.Double(13.0, 3.0, (double) getWidth() - 26.0, (double) getHeight() - 6.0, 10, 10);
        g2.fill(background);

        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, listBorderOpacity));
        Stroke stroke = g2.getStroke();
        g2.setStroke(stroke);

        g2.setComposite(composite);

        super.paintComponent(g);
    }

    public Color getColorBackGround() {
        return colorBackGround;
    }

    public void setColorBackGround(Color colorBackGround) {
        this.colorBackGround = colorBackGround;
    }

//    public class SelectorsListCellRenderer extends DefaultListCellRenderer {
//
//        private boolean isSelected;
//        private int index;
//        CashBoxSeparator separator;
//        final String SEPARATOR = "SEPARATOR";
//
//        public SelectorsListCellRenderer() {
//            super();
//        }
//
//        @Override
//        public Dimension getPreferredSize() {
//            Dimension size = super.getPreferredSize();
//            return size;
//        }
//
//        @Override
//        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
//
//            this.index = index;
//            this.isSelected = isSelected;
//
//            separator = new CashBoxSeparator(CashBoxSeparator.HORIZONTAL);
//
//            String titulo = value.toString() == null ? "" : value.toString();
//            super.getListCellRendererComponent(list, titulo, index, isSelected, cellHasFocus);
//            setOpaque(false);
//
//            setForeground(listItemColor);
//            if (SEPARATOR.equals(titulo)) {
//                return separator;
//            }
//
//            return this;
//
//        }
//
//        @Override
//        protected void paintComponent(Graphics g) {
//            Graphics2D g2 = (Graphics2D) g;
//
//            if (isSelected) {
//                paintSelection(g2);
//            }
//            paintText(g2);
//        }
//
//        private void paintSelection(Graphics2D g2) {
//            double y = 1.0;
//            double height = (double) getHeight() + 50;
//            double width = (double) getWidth() + 50;
//            RoundRectangle2D background;
//
//            Composite composite = g2.getComposite();
//            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionOpacity));
//            g2.setColor(selectionColor);
//
//            background = new RoundRectangle2D.Double(0, y, width, height, 0, 0);
//
//            g2.fill(background);
//
//            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionBorderOpacity));
//            Stroke stroke = g2.getStroke();
//            g2.setStroke(new BasicStroke(3.0f));
//            g2.draw(background);
//            g2.setStroke(stroke);
//
//            g2.setComposite(composite);
//        }
//
//        private void paintText(Graphics2D g2) {
//            FontMetrics fm = getFontMetrics(getFont());
//            int x = getInsets().left + 10;
//            int y = getInsets().top + fm.getAscent();
//
//            Composite composite = g2.getComposite();
//
//            TextLayout layout = new TextLayout(getText(), getFont(), g2.getFontRenderContext());
//            layout.draw(g2, x, y);
//
//            if (isSelected) {
//                g2.setComposite(composite);
//            }
//
//            g2.setColor(getForeground());
//        }
//    }
    public class MyListCellThing extends JLabel implements ListCellRenderer {

        private boolean isSelected;
        private int index;
        public MyListCellThing() {
            super();
            setOpaque(true);
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

            this.index = index;
            this.isSelected = isSelected;
            String titulo = value.toString() == null ? "" : value.toString();
            //getListCellRendererComponent(list, titulo, index, isSelected, cellHasFocus);
            setOpaque(false);

            setForeground(listItemColor);

            return this;

        }

        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            return size;
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;

            if (isSelected) {
                paintSelection(g2);
            }
            paintText(g2);
        }

        private void paintSelection(Graphics2D g2) {
            double y = 1.0;
            double height = (double) getHeight() + 50;
            double width = (double) getWidth() + 50;
            RoundRectangle2D background;

            Composite composite = g2.getComposite();
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionOpacity));
            g2.setColor(selectionColor);

            background = new RoundRectangle2D.Double(0, y, width, height, 0, 0);

            g2.fill(background);

            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, selectionBorderOpacity));
            Stroke stroke = g2.getStroke();
            g2.setStroke(new BasicStroke(3.0f));
            g2.draw(background);
            g2.setStroke(stroke);

            g2.setComposite(composite);
        }

        private void paintText(Graphics2D g2) {
            FontMetrics fm = getFontMetrics(getFont());
            int x = getInsets().left + 10;
            int y = getInsets().top + fm.getAscent();

            Composite composite = g2.getComposite();

            TextLayout layout = new TextLayout(getText(), getFont(), g2.getFontRenderContext());
            layout.draw(g2, x, y);

            if (isSelected) {
                g2.setComposite(composite);
            }

            g2.setColor(getForeground());
        }
    }
}